package com.txsj.scan.utils;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

/**
 * author:hs
 * des:隐藏显示输入法工具类
 */
public class SoftInputUtils {

    /*view为接受软键盘输入的视图，SHOW_FORCED表示强制显示*/
    public static void showSoftInput(Context context, View view) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
    }

    /*强制隐藏键盘*/
    public static void hideSoftInput(Context context, View view) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    /*强制隐藏键盘*/
    public static void hideSoftInput(Context context, EditText editText) {
        InputMethodManager inputmangers = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputmangers.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }

    /*调用隐藏系统默认的输入法*/
    public static void hideSystemSoftInput(Activity activity) {
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    }

    /*输入法显示的时候,调用此方法则隐藏,隐藏的时候调用此方法则显示*/
    public static void hideSoftInput(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

    }

    /*获取输入法打开的状态*/
    public static boolean isShowSoftInput(Context context) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        return imm.isActive();//true 打开
    }
}
